package com.hxzy.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.core.domain.TreeSelect;
import com.hxzy.common.core.domain.entity.CmsCategory;
import com.hxzy.common.core.domain.entity.SysConfig;
import com.hxzy.common.core.domain.entity.SysDictType;
import com.hxzy.common.core.domain.entity.SysMenu;
import com.hxzy.system.domain.dto.CategoryListDto;
import com.hxzy.system.mapper.CmsCategoryMapper;
import com.hxzy.system.mapper.SysConfigMapper;
import com.hxzy.system.service.CategoryService;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 64689
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CmsCategoryMapper, CmsCategory> implements CategoryService {




    @Override
    public List<CmsCategory> selectMenuList(CategoryListDto categoryListDto) {
        //数据库
        List<CmsCategory> allList= this.baseMapper.selectMenuList(categoryListDto);

        //自定义
        List<CmsCategory> buildList=new ArrayList<>();

         //组装成树形系
         allList.stream().filter(p->p.getCategoryId().intValue()==0).forEach(p->{
            recursionFn(allList,p);
             buildList.add(p);
        });

         return buildList;
    }
    /**
     * 构建前端所需要下拉树结构
     * @param menus 菜单列表
     * @return
     */
    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<CmsCategory> menus) {
        List<CmsCategory> cmsCategories=new ArrayList<>();
        //组装成树形系欸但
        menus.stream().filter(p->p.getCategoryId().intValue()==0).forEach(p->{
            recursionFn(menus,p);
            cmsCategories.add(p);
        });
        //再把 cmsCategories转换成 TreeSelect
        List<TreeSelect> collect=cmsCategories.stream().map(TreeSelect::new).collect(Collectors.toList());
        return collect;
    }

    /**
     * 验证栏目管理的唯一性
     * @param categoryTitle
     * @return
     */
    @Override
    public CmsCategory selectByTitle(String categoryTitle) {
        return this.baseMapper.selectByTitle(categoryTitle);
    }



    @Override
    public List<SysDictType> selectTitleAll() {
        return this.baseMapper.selectTitleAll();
    }

    @Override
    public CmsCategory selectDeptById(Long id) {
        return this.baseMapper.selectDeptById(id);
    }

    @Override
    public boolean updateBy(CmsCategory category) {
        return this.baseMapper.updateBy(category);
    }

    @Override
    public boolean hasChildById(Long id) {
        return this.baseMapper.hasChildById(id);
    }

    @Override
    public List<CmsCategory> selectBy(String categoryTitle) {
        return this.baseMapper.selectBy(categoryTitle);
    }

    /**
     * 根据上级节点，查询它的下级子节级
     * @param list  所有的数据库值
     * @param root  上级节点
     */
    private void recursionFn(List<CmsCategory> list, CmsCategory root) {
        List<CmsCategory>  childrenList=new ArrayList<>();

        //根据上级id  == 查询下面 parentId
        list.stream().filter( p -> p.getCategoryId().intValue() ==root.getId().intValue()).forEach( p-> {
            //当前子节点可能还有子节点
            recursionFn(list, p);

            childrenList.add(p);
        });


        //什么情况root有子节点的
        if(childrenList.size()>0){
            root.setChildren(childrenList);
        }
    }
}
